//
//  LPTLoginViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/3.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTLoginViewController.h"
#import "LPTBaseRequest.h"
#import "MMProgressHUD.h"
#import "DoctorBarCodeViewController.h"
#import "AppDelegate.h"

@interface LPTLoginViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIScrollView *mainScroll;

@property (nonatomic, weak) IBOutlet UITextField *phoneTextField;
@property (nonatomic, weak) IBOutlet UITextField *passTextField;


@end

@implementation LPTLoginViewController

+ (instancetype)instance
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    LPTLoginViewController *loginVC = [storyBoard instantiateInitialViewController];
    return loginVC;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    BOOL isNotFirstOpen = [[NSUserDefaults standardUserDefaults] boolForKey:@"FIRST_OPEN"];
    
    if (!isNotFirstOpen) {
        [self addFullPic];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FIRST_OPEN"];
    }
    
//    self.phoneTextField.text = @"16637649242";
//    self.passTextField.text = @"123456";
}

// 添加初次见面，轮播图
-(void)addFullPic
{
    self.mainScroll = [[UIScrollView alloc] init];
    self.mainScroll.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.mainScroll.pagingEnabled = YES;
    self.mainScroll.delegate = self;
    self.mainScroll.tag = 10;
    [self.mainScroll setContentSize:CGSizeMake(kScreenWidth*3, kScreenHeight)];
    self.mainScroll.showsHorizontalScrollIndicator = NO;
    self.mainScroll.backgroundColor = [UIColor blackColor];
    [([UIApplication sharedApplication].delegate).window addSubview:self.mainScroll];
    
    for (int i=0; i<3; i++) {
        NSString *imgStr = [NSString stringWithFormat:@"bg_default_%d" , i + 1];
        UIImage *imageS = [UIImage imageNamed:imgStr];
        UIImageView *imageSV = [[UIImageView alloc] init];
        imageSV.frame = CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight);
        imageSV.image = imageS;
        [self.mainScroll addSubview:imageSV];
        
        if (i == 2) {
            UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth * 2 + (kScreenWidth - 120 - 20), kScreenHeight - 60 - 50, 120, 60)];
            btn.backgroundColor = [UIColor clearColor];
            [btn setTitle:@"进入喜郎中" forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"default_btn_into"] forState:UIControlStateNormal];

            [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, - btn.imageView.image.size.width, 0, btn.imageView.image.size.width)];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.bounds.size.width)];
            
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(comeinAction) forControlEvents:UIControlEventTouchUpInside];
            [self.mainScroll addSubview:btn];
        }
    }
  
    _pageControl = [[UIPageControl alloc] init];
    self.pageControl.center = CGPointMake(kScreenWidth/2, kScreenHeight - 40);
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:155/255.0 green:233/255.0 blue:207/255.0 alpha:1];
    
    [([UIApplication sharedApplication].delegate).window addSubview:self.pageControl];
    
}

// 移除启动页
- (void)comeinAction
{
    if (self.mainScroll) {
        [self.mainScroll removeFromSuperview];
    }
    if (self.pageControl) {
        [self.pageControl removeFromSuperview];
    }
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger pageIndex = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage = pageIndex;
    if (scrollView.contentOffset.x > (kScreenWidth * 2 + kScreenWidth/3)) {
        [self comeinAction];
    }
}

- (IBAction)loginAction:(id)sender
{
//    DoctorBarCodeViewController *vc = [DoctorBarCodeViewController instance];
//    [self.navigationController pushViewController:vc animated:YES];
    
    [self requestLogin];
}

- (void)requestLogin
{
    [MMProgressHUD showWithStatus:@"登录中"];

    if (self.phoneTextField.text.length == 0 || self.passTextField.text.length == 0) {

        [MMProgressHUD dismissWithError:@"请输入账户和密码"];
        
        return;
    }
    
    NSDictionary *body = @{
                           @"mobile": self.phoneTextField.text,
                           @"password": self.passTextField.text
                           };
    
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/login" withBody:body withMethod:YTKRequestMethodPOST];
    request.needLogin = NO;
    
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismiss];
        
        [[LTPCacheManager shareInstance] setToken:data[@"token"]];

        [self dismissViewControllerAnimated:NO completion:NULL];
        
        [((AppDelegate*)[UIApplication sharedApplication].delegate) loadMainVC];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh_login_userinfo" object:nil];
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [MMProgressHUD dismissWithError:request.msg];
        
    }];
}



@end
